import multiprocessing
import threading
import time

from fake_useragent import UserAgent
import requests


ua = UserAgent().random
headers = {
    "referer": "https://www.gaokao.cn/",
    "user-agent": ua,
}


def thread2(school_detail_datas):
    while True:
        if len(school_detail_datas) > 0:
            url = school_detail_datas.pop()
            response = requests.post(url,headers=headers)
            res = response.json()
            major_list = res['data']['item']
            for major in major_list:
                school_name = major['name']
                school_id = major['school_id']
                average_score = major['average']
                major_name = major['spname']
                max_score = major['max']
                min_score = major['min']
                local_batch_name = major['local_batch_name']
                school_major_information = [school_name, school_id, average_score, major_name, max_score, min_score, local_batch_name]
                print(school_major_information)
                with open('school_major_information.txt', 'a', encoding='utf-8') as f:
                    f.write(str(school_major_information) + '\n')
                time.sleep(1)
        else:
            break





def thread1(datas):
    while True:
        if len(datas) > 0:
            url = datas.pop()
            response = requests.post(url,headers=headers)
            res = response.json()
            school_list = res['data']['item']
            if len(school_list) < 20:
                break
            school_detail_datas = [f'https://api.zjzw.cn/web/api/?local_batch_id=7&local_province_id=41&local_type_id=1&page=1&school_id={str(school["school_id"])}&size=10&sp_xuanke=&special_group=&uri=apidata/api/gk/score/special&year=2023&signsafe=8e3d3f926f92314db4cd08c3f1c3ba35'
                                   for school in school_list]
            thread1_list = []
            for i in range(5):
                t = threading.Thread(target=thread2, args=(school_detail_datas,))

                thread1_list.append(t)
                t.start()

            for t in thread1_list:
                t.join()
        else:
            break
    print(f"进程结束", threading.get_ident())


def main():
    print('主进程开始', multiprocessing.current_process().ident)  # 主进程
    datas = [
        f"https://api.zjzw.cn/web/api/?city_id=&keyword=&nature=&page={page}&province_id=&request_type=1&school_tese=&school_type=&size=20&sort=ruanke_rank&sorttype=asc&top_school_id=3703,3117,597,1724&type=&uri=apidata/api/gkv3/school/lists&signsafe=a837d17ca050c006f36a7fdf6f1ef517"
        for page in range(1, 148)]
    thread_list = []
    for i in range(5):
        t = threading.Thread(target=thread1, args=(datas,))

        thread_list.append(t)
        t.start()

    for t in thread_list:
        t.join()
    print('主进程结束')  # 主进程


if __name__ == '__main__':
    main()